package com.polaris.live.common.util

import android.annotation.SuppressLint
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.TimeUtils
import com.hjq.language.MultiLanguages
import com.polaris.live.common.appContext
import com.polaris.live.common.ext.convertArabicDigitsToNormal
import com.polaris.live.translation.R
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.math.abs

/**
 * 时间戳转为时间格式
 *
 * @author Created by 半仙 on 2023/8/15/015 12:03
 */
class DateUtils {

    companion object {

        fun formatDuration(timeMs: Long): String {
            return if (AppCodeUtils.isAR()) {
                val allLiveTime = formatDurationTime(timeMs)
                val parts = allLiveTime.split(":")
                val hours: Int
                val minutes: Int
                val seconds: Int

                when (parts.size) {
                    3 -> {
                        // 格式为 HH:mm:ss
                        hours = parts[0].toInt()
                        minutes = parts[1].toInt()
                        seconds = parts[2].toInt()
                    }

                    2 -> {
                        // 格式为 mm:ss
                        hours = 0
                        minutes = parts[0].toInt()
                        seconds = parts[1].toInt()
                    }

                    else -> {
                        // 格式错误 返回本身
                        return allLiveTime
                    }
                }

                return String.format("%02d:%02d:%02d", hours, minutes, seconds)
                    .convertArabicDigitsToNormal()
            } else {
                formatDurationTime(timeMs)
            }
        }

        fun formatDurationTime(timeMs: Long): String {
            var temptimeMs = timeMs
            val prefix = if (temptimeMs < 0) "-" else ""
            temptimeMs = abs(temptimeMs)
            val totalSeconds = temptimeMs / 1000
            val seconds = totalSeconds % 60
            val minutes = totalSeconds / 60 % 60
            val hours = totalSeconds / 3600
            return if (hours > 0) String.format(
                MultiLanguages.getSystemLanguage(appContext),
                "%s%d:%02d:%02d",
                prefix,
                hours,
                minutes,
                seconds
            ) else String.format(
                MultiLanguages.getSystemLanguage(appContext),
                "00:%s%02d:%02d",
                prefix,
                minutes,
                seconds
            )
        }

        /**
         * 秒转成分钟
         */
        fun convertSecondsToMinutes(seconds: Long): Long {
            return seconds / 60
        }

        fun minuteSChangeTime(timeMs: Long): String {
            var temptimeMs = timeMs
            val prefix = if (temptimeMs < 0) "-" else ""
            temptimeMs = abs(temptimeMs)
            val totalSeconds = temptimeMs / 1000
            val seconds = totalSeconds % 60
            val minutes = totalSeconds / 60 % 60
            val hours = totalSeconds / 3600

            return String.format("%02d:%02d:%02d", hours, minutes, seconds)
        }

        @SuppressLint("SimpleDateFormat")
        fun formatDateString(
            dateString: String,
        ): String? {
            val inputDateFormat = SimpleDateFormat("yyyy-M-d")
            val outputDateFormat = SimpleDateFormat("yyyy-MM-dd")
            try {
                val date: Date = inputDateFormat.parse(dateString) ?: return null
                return outputDateFormat.format(date)
            } catch (e: ParseException) {
                e.printStackTrace()
            }
            return null
        }

        fun convertTimestampToString(timestamp: Long): String? {
            return TimeUtils.millis2String(timestamp, "yyyy-MM-dd")
        }

        //判断两个时间戳是否是一天
        fun isOneDay(lastTime: Long, nowTime: Long): Boolean {
            val calendar1 = Calendar.getInstance().apply {
                timeInMillis = lastTime
            }

            val calendar2 = Calendar.getInstance().apply {
                timeInMillis = nowTime
            }

            return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) &&
                    calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH) &&
                    calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)
        }

        /**
         * 判断是否大于两天
         */
        fun isMoreThanTwoDays(timestamp1: Long, timestamp2: Long): Boolean {
            val diff = Math.abs(timestamp1 - timestamp2)
            val twoDaysInMillis = 2 * 24 * 60 * 60 * 1000 // 2天的毫秒数
            return diff > twoDaysInMillis
        }

        fun formatRemainingNumber(millis: Long): String {
            val seconds = millis / 1000
            val minutes = seconds / 60
            val hours = minutes / 60
            val days = hours / 24
            val years = days / 365

            return when {
                years > 0 -> "${years}year"
                days > 0 -> "${days}days"
                else -> "${hours}h"
            }
        }

        fun formatTime(timeInMillis: Long): String {
            val days = TimeUnit.MILLISECONDS.toDays(timeInMillis)
            val hours = TimeUnit.MILLISECONDS.toHours(timeInMillis)
            val minutes = TimeUnit.MILLISECONDS.toMinutes(timeInMillis)
            val seconds = TimeUnit.MILLISECONDS.toSeconds(timeInMillis)
            return when {
                days > 0 -> StringUtils.getString(R.string.days, days)
                hours > 0 -> StringUtils.getString(R.string.hours, hours)
                minutes > 0 -> StringUtils.getString(R.string.minutes, minutes)
                else -> StringUtils.getString(R.string.seconds, seconds)
            }
        }
    }

}